#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>

#define FNAME "/tmp/out"

static int daemonize(void)
{
	int fd;
	pid_t pid;

	pid = fork();
	if(pid < 0)
		return -1;;
	if(pid == 0)
	{
		fd = open("/dev/null", O_RDWR);
		if(fd < 0)
			return -1;

		dup2(fd, 0);
		dup2(fd, 1);
		dup2(fd, 2);
		if(fd > 2)
			close(fd);

		setsid();

		chdir("/");
		umask(0);

		return 0;
	}
	if(pid > 0)
	{
		exit(0);
	}
	return -1;
}

int main()
{
	FILE * fp;
	int i;

	openlog("mydaemon", LOG_PID, LOG_DAEMON);

	if(daemonize() < 0)
	{
		syslog(LOG_ERR, "daemonize failed!");
		exit(1);
	}
	else
		syslog(LOG_INFO, "daemonize sucessed!");

	fp = fopen(FNAME, "w");
	if(fp == NULL)
	{
		syslog(LOG_ERR, "fopen:%s", strerror(errno));
		exit(1);
	}
	else
		syslog(LOG_INFO, "%s was opened!", FNAME);

	for(i = 0; ; i++)
	{
		fprintf(fp, "%d", i);
		fflush(fp);
		syslog(LOG_DEBUG, "%d is printed", i);
		sleep(1);
	}

	fclose(fp);

	closelog();

	exit(0);
}
